﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class AddNewP
    Inherits System.Web.UI.Page

    Public scripttext As String
    Dim lockhash As New Hashtable

    Private Shared logger As ILog = LogManager.GetLogger("FileAppender")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Session("users") Is Nothing Then
            scripttext = "<script> ClosePop();</script>"
        End If

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(Session("PlanYear")) & " "
        rsql += " AND Version = '" & Session("Versions").ToString & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "
        'Response.Write(rsql)
        'Exit Sub
        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next
    End Sub

    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim ds As New DataSet
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If fctr <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & fctr & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
            'Response.Write(sql & "<br />")
        Else
            'ds = PublicFunction.GetChild2(Session("users"))
            'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
            'e.DataSource = ds
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
        End If


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub ProductCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProductCombo.InitializeDataSource
        Dim adap As New SplitBudgetDSTableAdapters.ProductsTableAdapter
        Dim dt As New SplitBudgetDS.ProductsDataTable
        Dim formid As String = Request("FormID")
        If formid = "P03" Then
            dt = adap.GetDataByP03(Val(Session("PlanYear")))
            e.DataSource = dt
        Else
            dt = adap.GetDataByP04(Val(Session("PlanYear")))
            e.DataSource = dt
        End If


    End Sub

    Protected Sub SaveDocument_Click(sender As Object, e As EventArgs) Handles SaveDocument.Click
        If FundCenterCombo.Value = "" Then
            scripttext = "<script> alert('กรุณาเลือก Fund Center');</script>"
            Exit Sub
        End If
        If ProductCombo.Value = "" Then
            scripttext = "<script> alert('กรุณาเลือกผลิตภัณฑ์');</script>"
            Exit Sub
        End If
        If lockhash(FundCenterCombo.Value.ToString.Trim) = True Then
            scripttext = "<script> alert('ไม่สามารถบันทึกได้ เนื่องจากมีการส่งแผนไปแล้ว');</script>"
            Exit Sub
        End If

        Dim adapter1 As New MSinchuaTableAdapters.BudgetRevPlans1TableAdapter
        Dim adapter2 As New PDataSetTableAdapters.ProductRevsTableAdapter
        Dim adapter3 As New PDataSetTableAdapters.BudgetDetailsTableAdapter
        Dim adapter4 As New MSinchuaTableAdapters.ProductItemsTableAdapter
        Dim adapter5 As New PDataSetTableAdapters.ProductItemRevsTableAdapter
        Dim dataX As New MSinchua.BudgetRevPlans1DataTable
        Dim counterX As Decimal = 0
        counterX = adapter1.CheckExists(ProductCombo.Value, FundCenterCombo.Value, Val(Session("PlanYear")), Session("Versions").ToString, Request("FormID"))


        If counterX = 0 Then
            Dim data3 As New MSinchua.ProductItemsDataTable
            data3 = adapter4.GetDataByFormID(Request("FormID"), Val(Session("PlanYear")))
            If data3.Count = 0 Then
                scripttext = "<script>alert('ไม่มี ProductItem ที่ตรงกัน')</script>"
                Exit Sub
            Else
                Dim adapx As New PextraTableAdapters.ProductItemsTableAdapter
                Dim itemno As Integer = 0
                For Each row2 As MSinchua.ProductItemsRow In data3
                    itemno = adapx.GetItemNo(row2.Id)
                    Dim cmmtcode As String = ""
                    If Request("FormID") = "P04" Then
                        'Dim adap As New DBdataTableAdapters.AppConfigsTableAdapter
                        'Dim dt As New DBdata.AppConfigsDataTable
                        'dt = adap.GetDataByName("P04_CMMT_CODE")
                        'For Each item As DBdata.AppConfigsRow In dt
                        '    cmmtcode = item.Value
                        'Next

                        cmmtcode = adapter2.GetCMmtCode(ProductCombo.Value, row2.Id)
                    Else
                        Try
                            cmmtcode = adapter2.GetCMmtCode(ProductCombo.Value, row2.Id)
                        Catch ex As Exception
                            logger.Error(ex.Message)
                            logger.Error(ex.StackTrace)
                        End Try
                    End If


                    Dim sum1 = adapter3.GetSumLast1(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value)
                    Dim Actual1 As Decimal
                    Dim Actual2 As Decimal
                    Dim Actual3 As Decimal
                    Dim Actual4 As Decimal
                    Dim Actual5 As Decimal
                    Dim Actual6 As Decimal
                    Dim Actual7 As Decimal
                    Dim Actual8 As Decimal
                    Dim Actual9 As Decimal
                    Dim Actual10 As Decimal
                    Dim Actual11 As Decimal
                    Dim Actual12 As Decimal

                    Dim CActual1 As Decimal
                    Dim CActual2 As Decimal
                    Dim CActual3 As Decimal
                    Dim CActual4 As Decimal
                    Dim CActual5 As Decimal
                    Dim CActual6 As Decimal
                    Dim CActual7 As Decimal
                    Dim CActual8 As Decimal
                    Dim CActual9 As Decimal
                    Dim CActual10 As Decimal
                    Dim CActual11 As Decimal
                    Dim CActual12 As Decimal

                    If Request("FormID") = "P03" Then

                        If itemno = 6 Then
                            Dim dt As New MSinchua.BudgetRevPlans1DataTable
                            dt = adapter1.GetDataP03FROMP02(ProductCombo.Value, FundCenterCombo.Value, Val(Session("PlanYear")), Session("Versions").ToString)
                            Try

                                Actual1 = dt.Item(0).LastYearMonth1
                                Actual2 = dt.Item(0).LastYearMonth2
                                Actual3 = dt.Item(0).LastYearMonth3
                                Actual4 = dt.Item(0).LastYearMonth4
                                Actual5 = dt.Item(0).LastYearMonth5
                                Actual6 = dt.Item(0).LastYearMonth6
                                Actual7 = dt.Item(0).LastYearMonth7
                                Actual8 = dt.Item(0).LastYearMonth8
                                Actual9 = dt.Item(0).LastYearMonth9
                                Actual10 = dt.Item(0).LastYearMonth10
                                Actual11 = dt.Item(0).LastYearMonth11
                                Actual12 = dt.Item(0).LastYearMonth12

                                CActual1 = dt.Item(0).CurYearMonth1
                                CActual2 = dt.Item(0).CurYearMonth2
                                CActual3 = dt.Item(0).CurYearMonth3
                                CActual4 = dt.Item(0).CurYearMonth4
                                CActual5 = dt.Item(0).CurYearMonth5
                                CActual6 = dt.Item(0).CurYearMonth6
                                CActual7 = dt.Item(0).CurYearMonth7
                                CActual8 = dt.Item(0).CurYearMonth8
                                CActual9 = dt.Item(0).CurYearMonth9
                                CActual10 = dt.Item(0).CurYearMonth10
                                CActual11 = dt.Item(0).CurYearMonth11
                                CActual12 = dt.Item(0).CurYearMonth12
                            Catch ex As Exception
                                Actual1 = 0
                                Actual2 = 0
                                Actual3 = 0
                                Actual4 = 0
                                Actual5 = 0
                                Actual6 = 0
                                Actual7 = 0
                                Actual8 = 0
                                Actual9 = 0
                                Actual10 = 0
                                Actual11 = 0
                                Actual12 = 0

                                CActual1 = 0
                                CActual2 = 0
                                CActual3 = 0
                                CActual4 = 0
                                CActual5 = 0
                                CActual6 = 0
                                CActual7 = 0
                                CActual8 = 0
                                CActual9 = 0
                                CActual10 = 0
                                CActual11 = 0
                                CActual12 = 0
                            End Try
                        Else
                            Actual1 = 0
                            Actual2 = 0
                            Actual3 = 0
                            Actual4 = 0
                            Actual5 = 0
                            Actual6 = 0
                            Actual7 = 0
                            Actual8 = 0
                            Actual9 = 0
                            Actual10 = 0
                            Actual11 = 0
                            Actual12 = 0

                            CActual1 = 0
                            CActual2 = 0
                            CActual3 = 0
                            CActual4 = 0
                            CActual5 = 0
                            CActual6 = 0
                            CActual7 = 0
                            CActual8 = 0
                            CActual9 = 0
                            CActual10 = 0
                            CActual11 = 0
                            CActual12 = 0
                        End If

                    ElseIf Request("FormID") = "P04" Then
                        Try
                            Actual1 = adapter3.GetValue1_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                            Actual2 = adapter3.GetValue2_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                            Actual3 = adapter3.GetValue3_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                            Actual4 = adapter3.GetValue4_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                            Actual5 = adapter3.GetValue5_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        Catch ex As Exception
                            Actual1 = 0
                            Actual2 = 0
                            Actual3 = 0
                            Actual4 = 0
                            Actual5 = 0
                        End Try

                        Actual6 = 0
                        Actual7 = 0
                        Actual8 = 0
                        Actual9 = 0
                        Actual10 = 0
                        Actual11 = 0
                        Actual12 = 0

                        CActual1 = 0
                        CActual2 = 0
                        CActual3 = 0
                        CActual4 = 0
                        CActual5 = 0
                        CActual6 = 0
                        CActual7 = 0
                        CActual8 = 0
                        CActual9 = 0
                        CActual10 = 0
                        CActual11 = 0
                        CActual12 = 0
                    End If
                    Dim sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
                    adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Val(Session("Versions")), Val(sum1), Val(sum2), 0, 0, Actual1, Actual2, Actual3, Actual4, Actual5, Actual6, Actual7, Actual8, Actual9, Actual10, Actual11, Actual12, CActual1, CActual2, CActual3, CActual4, CActual5, CActual6, CActual7, CActual8, CActual9, CActual10, CActual11, CActual12, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), row2.Id)
                Next
            End If

        Else
            scripttext = "<script>alert('ไม่สามารถเพิ่มข้อมูลซ้ำได้')</script>"
            Exit Sub
        End If

        scripttext = "<script>ClosePop();</script>"
        'Response.Write(ProductCombo.Value)

    End Sub
End Class
